package ui.videos 
{
	import com.gaiaframework.api.Gaia;
	import com.gaiaframework.api.IPageAsset;
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import model.Model;
	import pages.Pages;
	import utils.StringUtils;
	import utils.TextFieldUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class NavTimeLine extends MovieClip 
	{
		private var _xmlData:XML;
		private var _nav:MovieClip;
		private var _chaptersArray:Array;
		private var _currentTagsArray:Array;
		private var _cb:String;
		private var _cp:IPageAsset;
		private var _ci:String;
		private var _tag:String;
		private var  _obj:*;
		private var _frise:Frise;
		private var _indicate:TextField;
		public function NavTimeLine() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		public function destroy():void 
		{
			if (this.parent != null) parent.removeChild(this);
		}
		
		public function update(obj:*):void 
		{
			_currentTagsArray = [];
			_obj = obj;
			_cb = Gaia.api.getCurrentBranch();
			_cp = Gaia.api.getPage(_cb);
			_ci = _cp.id;
			_tag = obj.name;
			var ln:int = _chaptersArray.length;
			for (var i:int = 0; i < ln; i++) 
			{
				if (_chaptersArray[i].tag==_tag)
				{
					_currentTagsArray.push(_chaptersArray[i]);
				}			
			}
			if (!_frise)
			{
				addFrise();
			}
			if (_frise.name != _tag)
			{
				removeChild(_frise);
				_frise = null;
				addFrise();
			}
			_frise.update(_obj);
			
			var title:String = Model.instance.getUiLabel(_ci + '-indicate');
			TextFieldUtil.setText(_indicate, '<indicate>'+title+'</indicate>');
			//if(Gaia.api.getPage(Pages.NAV).content.)
			resize();
			//arrangeFriseByTag();
		}
		
		public function resize():void 
		{
			var sw:Number = stage.stageWidth;
			var sh:Number = stage.stageHeight;
			if (_frise)
			{
				_frise.x = 0;
				_frise.y = int(sh - 197);
				//_frise.y = 0;
				_frise.resize();
			}
			if (_indicate)
			{
				_indicate.y = sh - _indicate.height -5;
				_indicate.x = 5
			}
		}
		
		public function updateTimelinePosition(ev:String):void 
		{
			trace("[NavTimeLine.updateTimelinePosition] ", ev, 'update');
			TweenMax.killTweensOf(this);
			TweenMax.killTweensOf(_frise);
			TweenMax.killTweensOf(_indicate);
			if (ev == "over")
			{
				this.alpha = 1;
				TweenMax.to(_frise, .5, { autoAlpha:1, delay:.5 } );
				TweenMax.to(_indicate, .5, { alpha:0 } );
			}
			
			if (ev == "out")
			{
				this.alpha = 1;
				TweenMax.to(_frise, 0, { autoAlpha:0 } );
				
				TweenMax.to(_indicate, .5, { alpha:1, delay:.3 } );
			}
			
		}
		
		private function addFrise():void 
		{
			_frise = new Frise(_currentTagsArray,_obj);
			_frise.name = _tag;
			TweenMax.to(_frise, 0, { autoAlpha:0 } );
			addChild(_frise);
		}
		
		private function arrangeFriseByTag():void 
		{
			var ln:int = _currentTagsArray.length;
			var currentVideoNumber:int
			for (var i:int = 0; i < ln; i++) 
			{
				if ( _currentTagsArray[i].pageId == _ci) currentVideoNumber = i;
			}
			trace("[NavTimeLine.arrangeFriseByTag] TAG="+_tag+" VideoNumber="+ln+" CurrentVideoId="+_ci+' CurrentVideoNumber='+currentVideoNumber);
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			_nav = Gaia.api.getPage(Pages.NAV).content;
			_xmlData = Gaia.api.getPage(Pages.TIMELINE).node;
			addChapters();
			addTextField();
		}
		
		private function addTextField():void 
		{
			_indicate = new TextField();
			_indicate.mouseEnabled = false;
			_indicate.alpha = 0;
			_indicate.selectable = false;
			addChild(_indicate)
		}
		
		private function addChapters():void 
		{
			_chaptersArray = [];
			var ln:int = _xmlData.page.length();
			for (var i:int = 0; i < ln; i++) 
			{
				var assetsLength:int = _xmlData.page[i].asset.length();
				for (var j:int = 0; j < assetsLength; j++) 
				{
					var page:XML = _xmlData.page[i];
					var pageId:String = page.@id;
					var item:Object = { };
					item.pageId = pageId;
					item.tag = page.asset[j].@id;
					item.title =  Model.instance.getUiLabel(pageId + '-timelineTitle');
					item.subtitle =  Model.instance.getUiLabel(pageId + '-timelineSubTitle');
					item.description =  Model.instance.getUiLabel(pageId + '-description');
					_chaptersArray.push(item);
				}
			}
		}
		
	}

}